/*
 * Copyright (c) 2015 Messcat. All rights reserved.
 * 
 */
package cc.messcat.common.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.util.LogUtil;

public class McExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

		HandlerMethod hm = (HandlerMethod) handler;
		String className = hm.getBeanType().getSimpleName();
		String method = hm.getMethod().getName();
		StringBuffer buffer = new StringBuffer(className);
		buffer.append(".").append(method).append(" exception: ");
		LogUtil.logException(buffer.toString(), ex);
		ModelAndView mv = new ModelAndView();
		FastJsonJsonView view = new FastJsonJsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("status", CommonConstant.EXCEPTION_CODE_500);
		attributes.put("message", CommonConstant.MSG_FAIL);
		view.setAttributesMap(attributes);
		mv.setView(view);
		return mv;
	}

}
